package com.zhuss.opus.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.opus.invit.CreamInvitBo;
import com.zhuss.bean.bo.opus.invit.UpdateInvitBo;
import com.zhuss.bean.vo.opus.invit.DetailInvitVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 优选帖子 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("invitCream")
public class InvitCream extends AbstractProcessor<Object, CreamInvitBo> implements Processor<Object, CreamInvitBo> {

    @Autowired
	@Qualifier("invitRead")
	private InvitRead invitRead;
    
    @Autowired
	@Qualifier("invitUpdate")
	private Processor<Object, UpdateInvitBo> invitUpdate;
    
    /**
     * 校验
     */
    @Override
    public void check(CreamInvitBo bo) throws WpsException {
    	Long invitId = bo.getInvitId();
    	Integer creamType = bo.getCream();
    	
    	DetailInvitVo result = ((DetailInvitVo) invitRead.detail(invitId));
    	
    	if (creamType.equals(result.getCream())) {
    		throw new WpsException(ExceptionCode.帖子已优选);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CreamInvitBo param) throws WpsException {
    	
    	Long invitId = param.getInvitId();
    	DetailInvitVo invit = ((DetailInvitVo) invitRead.detail(invitId));
    	String name = invit.getTitle();
    	
    	UpdateInvitBo updateBo = new UpdateInvitBo();
    	BeanUtils.copyProperties(invit, updateBo);
    	BeanUtils.copyProperties(param, updateBo);
    	
    	invitUpdate.process(updateBo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreamInvitBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.帖子.key());
    	record.setAction(LogActionEnum.帖子优选.key());
    	record.setTargetId(param.getInvitId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
